unit SmallStruct.View.TSSDataFormView;

interface

uses
  Windows,
  Messages,
  SysUtils,
  Variants,
  Classes,
  Graphics,
  Controls,
  Forms,
  SmallStruct.View.TSSDataView,
  cxContainer,
  cxEdit,
  ExtCtrls,
  RzPanel,
  SmallStruct.View,
  cxStyles,
  cxCustomData,
  cxGraphics,
  cxFilter,
  cxData,
  cxDataStorage,
  DB,
  cxDBData,
  cxGridLevel,
  cxClasses,
  cxControls,
  cxGridCustomView,
  cxGridCustomTableView,
  cxGridTableView,
  cxGridDBTableView,
  cxGrid,
  cxGridCardView,
  cxGridDBCardView,
  cxDBLookupComboBox;

type
  TSSDataFormView = class(TSSDataView, ISSDataFormView)
    _GridLevel: TcxGridLevel;
    _Grid: TcxGrid;
    _StyleRepository: TcxStyleRepository;
    _BackgroundStyle: TcxStyle;
    _ContentEvenStyle: TcxStyle;
    _GroupByBoxStyle: TcxStyle;
    _SelectionStyle: TcxStyle;
    _GridDBCardView: TcxGridDBCardView;
  public
    procedure InitObject; override;
    procedure InitLookupComboBoxColumn(ADataCard: TcxGridDBCardView; AColumnIndex: Integer; const ALinkeModelName, ALinkFieldName, ALinkKeyFieldName: string);
    procedure InitDataFormView(ADataCard: TcxGridDBCardView; const AModelName: string);
  end;

implementation

uses
  SmallStruct.Model,
  SmallStruct.Configuration;

{$R *.dfm}

{ TSSDataFormView }

procedure TSSDataFormView.InitDataFormView(ADataCard: TcxGridDBCardView; const AModelName: string);
var
  ViewConfiguration: ISSViewConfiguration;
  FieldConfiguration: ISSViewFieldConfiguration;
  Index: Integer;
begin
  ADataCard.DataController.DataSource := QueryDataModel(AModelName).GetDataSource;
  ADataCard.ClearItems;

  ViewConfiguration := GetContext.GetConfiguration as ISSViewConfiguration;

  for Index := 0 to ViewConfiguration.GetFieldConfiguration.GetCount - 1 do
  begin
    FieldConfiguration := ViewConfiguration.GetFieldConfiguration.GetObject(Index) as ISSViewFieldConfiguration;
    if FieldConfiguration.IsVisiable then
    begin
      ADataCard.CreateRow.DataBinding.FieldName := FieldConfiguration.GetFieldName;

      if (FieldConfiguration.GetLinkFieldName <> '') and (FieldConfiguration.GetLinkModelName <> '') and (FieldConfiguration.GetLinkKeyFieldName <> '') then
        InitLookupComboBoxColumn(ADataCard, Index, FieldConfiguration.GetLinkModelName, FieldConfiguration.GetLinkFieldName, FieldConfiguration.GetLinkKeyFieldName);
    end;
  end;
end;

procedure TSSDataFormView.InitLookupComboBoxColumn(ADataCard: TcxGridDBCardView; AColumnIndex: Integer; const ALinkeModelName, ALinkFieldName, ALinkKeyFieldName: string);
begin
  ADataCard.Rows[AColumnIndex].PropertiesClass := TcxLookupComboBoxProperties;
  with TcxLookupComboBoxProperties(ADataCard.Rows[AColumnIndex].Properties) do
  begin
    ListSource := GetDataSource(ALinkeModelName);
    ListFieldNames := ALinkFieldName;
    KeyFieldNames := ALinkKeyFieldName;
  end;
end;

procedure TSSDataFormView.InitObject;
begin
  inherited;
  InitDataFormView(_GridDBCardView, (GetContext.GetConfiguration as ISSViewConfiguration).GetPrimaryModelName);
end;

end.
